#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <functional>
#include <vector>
const int Threadnum = 5;
using func_t = std::function<void()>;
class ThreadData
{   
    public:
        ThreadData(const std::string& name, u_int64_t ctime, func_t func)
        :_name(name), _ctime(ctime), _func(func)
        {}
    public:
        std::string _name;
        u_int64_t _ctime;
        func_t _func;
};
void Print()
{
    std::cout << "我是一个线程， 我正在执行任务" << std::endl; 
}
void *ThreadRoutine(void *args)
{
    int a = 10;
    ThreadData* td = static_cast<ThreadData*>(args);
    std::cout << "Threadname:" << td->_name << "Time:" << td->_ctime << std::endl;
    td->_func();
    if(td->_name == "thread-4")
    {
        std::cout << "thread-4触发异常" << std::endl;
        //exit(0);
    }
    sleep(1);
}
int main()
{
    std::vector<pthread_t> tids;
    for (size_t i = 1;i <= Threadnum; i ++)
    {
        char Threadname[64];
        snprintf(Threadname, sizeof (Threadname), "%s-%lu", "thread", i);

        pthread_t tid;
        ThreadData* td = new ThreadData(Threadname, (u_int64_t)time(nullptr), Print);
        pthread_create(&tid, nullptr, ThreadRoutine, td);
        tids.push_back(tid);
        sleep(2);
    }
    std::cout << "Thread id is" << std::endl;
    for(auto id: tids)
    {
        std::cout << id << std::endl;
    }
    while(true)
    {
        std::cout << "main thread" << std::endl;
        sleep(2);
    }
    return 0;
}